<template>
    <div>
        <s-search-bar>
            <el-form :model="carOwner" label-width="80px">
                <el-form-item label="小区" prop="orgId" v-hasOrg>
                    <org-select v-model="carOwner.orgId"  @select="query"></org-select>
                </el-form-item>
                <el-form-item prop="ownerName" label="设备名称">
                    <el-input v-model="carOwner.alarmEquipmentName"></el-input>
                </el-form-item>
                <el-form-item prop="phone" label="设备编码">
                    <el-input v-model="carOwner.alarmEquipmentCode"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="addCarOwner" type="success">新增</el-button>
                    <el-button @click="query" icon="el-icon-search" type="primary">查询</el-button>
                </el-form-item>
            </el-form>
        </s-search-bar>

        <basic-table :tableLoading="tableLoading" :tableData="tableData" :tableOption="tableOption" :tablePage="tablePage" @table-search="query">
            <template slot="operation" slot-scope="scope">
                <!-- <el-button type="text" icon="el-icon-delete" @click="startAlarm(scope.scope.row,scope.scope.$index)">启动预警</el-button> -->
                <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.scope.row,scope.scope.$index)">修改</el-button>
                <el-button type="text" icon="el-icon-delete" @click="rowDel(scope.scope.row,scope.scope.$index)">删除</el-button>
            </template>
        </basic-table>
        <add-camera-equipment ref="add-alarm-equipment" @table-search="query"></add-camera-equipment>
    </div>
</template>


<script>
//车主管理
import AddCameraEquipment from './add.vue'
import { tableOption } from '@/const/crud/business/camera-equipment'
import * as API from '@/api/business/camera-equipment'
export default {
    components: { AddCameraEquipment },
    data() {
        return {
            tableData: [],
            tableOption: tableOption,
            tableLoading: false,
            tablePage: {
                size: 50,
                current: 1,
                total: 0
            },
            carOwner: {
                cameraEquipmentName: '',
                cameraEquipmentCode: '',
                orgId: this.$store.state.data.globalOrg.id,
            }
        }
    },
    methods: {
        addCarOwner() {
            this.$refs['add-alarm-equipment'].show()
        },
        query() {
            this.tableLoading = true
            API.page(Object.assign(this.tablePage,this.carOwner)).then(res => {
                this.tableLoading = false
                if (res.data.code === 0) {
                    this.tableData = res.data.data.records
                    this.tablePage.total = Number.parseInt(res.data.data.total)
                }
            })
        },
        handleEdit(row, index) {
            this.$refs['add-alarm-equipment'].edit(row)
        },
        rowDel(row, index) {
            this.$confirm('是否删除当前数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                API.deleteById(row.alarmEquipmentId).then(res => {
                    if(res.data.code === 0){
                        this.$message.success('删除成功')
                        this.query()
                    }
                })
            })
        },
        //启动摄像头预警
        startAlarm(row){
            API.login(row.cameraEquipmentId).then(res=>{
                if(res.data.code === 0){
                    this.$message.success('启动成功')
                }
            })
        }
    },
    mounted() {
        this.query()
    }
}
</script>
